package com.ruoyi.project.kkad.advertising.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.RedisKey;
import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.kkad.adplace.domain.AdvertisingPlace;
import com.ruoyi.project.kkad.adplace.mapper.AdvertisingPlaceMapper;
import com.ruoyi.project.kkad.advertising.domain.Advertising;
import com.ruoyi.project.kkad.advertising.mapper.AdvertisingMapper;
import com.ruoyi.project.kkad.advertising.service.IAdvertisingService;

/**
 * 广告管理Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-04-13
 */
@Service
@DataSource(DataSourceType.KKAD)
public class AdvertisingServiceImpl implements IAdvertisingService {

	@Autowired
	@Qualifier("commonRedisTemplate")
	RedisTemplate<String, String> redisTemplate;

	@Autowired
	private AdvertisingMapper advertisingMapper;

	@Autowired
	private AdvertisingPlaceMapper advertisingPlaceMapper;

	/**
	 * 查询广告管理
	 * 
	 * @param id 广告管理ID
	 * @return 广告管理
	 */
	@Override
	public Advertising selectAdvertisingById(Long id) {
		Advertising advertising = advertisingMapper.selectAdvertisingById(id);
		setPlace(advertising);
		return advertising;
	}

	private void setPlace(Advertising item) {

		AdvertisingPlace query = new AdvertisingPlace();
		query.setCode(item.getPlaceCode());
		List<AdvertisingPlace> advertisingPlaces = advertisingPlaceMapper.selectAdvertisingPlaceList(query);
		if (!advertisingPlaces.isEmpty()) {
			item.setPlace(advertisingPlaces.get(0));
		}

	}

	/**
	 * 查询广告管理列表
	 * 
	 * @param advertising 广告管理
	 * @return 广告管理
	 */
	@Override
	public List<Advertising> selectAdvertisingList(Advertising advertising) {
		List<Advertising> list = advertisingMapper.selectAdvertisingList(advertising);
		list.forEach(item -> {
			setPlace(item);
		});
		return list;
	}

	/**
	 * 新增广告管理
	 * 
	 * @param advertising 广告管理
	 * @return 结果
	 */
	@Override
	public int insertAdvertising(Advertising advertising) {
		return advertisingMapper.insertAdvertising(advertising);
	}

	/**
	 * 修改广告管理
	 * 
	 * @param advertising 广告管理
	 * @return 结果
	 */
	@Override
	public int updateAdvertising(Advertising advertising) {

		int effect = advertisingMapper.updateAdvertising(advertising);

		Advertising old = advertisingMapper.selectAdvertisingById(advertising.getId());

		String listKey = RedisKey.Ad.getAdvertisingListKey(old.getPlatform(), old.getAppId(), old.getPlaceCode());
		if (advertising.getStatus() == 1) {
			redisTemplate.opsForZSet().add(listKey, String.valueOf(old.getId()), old.getSequence());
		} else {
			redisTemplate.opsForZSet().remove(listKey, String.valueOf(old.getId()));
		}

		redisTemplate.opsForHash().put(RedisKey.Ad.getAdvertisingKey(), String.valueOf(old.getId()), JsonUtil.toJson(old));

		return effect;
	}

	/**
	 * 删除广告管理对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteAdvertisingByIds(String ids) {

		String[] strIds = Convert.toStrArray(ids);
		int count = 0;
		for (String strId : strIds) {
			count += this.deleteAdvertisingById(Long.parseLong(strId));
		}
		return count;
	}

	/**
	 * 删除广告管理信息
	 * 
	 * @param id 广告管理ID
	 * @return 结果
	 */
	@Override
	public int deleteAdvertisingById(Long id) {

		Advertising advertising = this.selectAdvertisingById(id);
		String listKey = RedisKey.Ad.getAdvertisingListKey(advertising.getPlatform(), advertising.getAppId(), advertising.getPlaceCode());
		redisTemplate.opsForZSet().remove(listKey, String.valueOf(advertising.getId()));

		return advertisingMapper.deleteAdvertisingById(id);
	}
}
